package com.cn.zpark;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @PackageName: com.cn.zpark
 * @ClassName: RequestDemo04
 * @Description:
 * @author: RZS
 * @date: 2022/4/12  20:17
 */
@WebServlet("/req4.do")
public class RequestDemo04 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 本servlet无法处理请求，需要进行转发
        // 设置响应数据类型
        response.setContentType("text/html;charset=utf-8");
        // 获取写出流
        PrintWriter writer = response.getWriter();

        writer.println("<h1>转发之前</h1>");

        // 绑定请求域数据
        request.setAttribute("name", "中关村软件园号称中国硅谷");
        request.setAttribute("url", "北京-海淀区");

        // 转发给demo05进行处理
        // 获取转发器
        RequestDispatcher dispatcher = request.getRequestDispatcher("./req5.do");
        // 转发
        dispatcher.forward(request, response);

        writer.close();
    }
}
